package com.lujun;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.sql.Time;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

public class MyFrame extends JFrame {
    public MyFrame() {}
    JTextField textField = null;
    JTextField outTextField = null;
    JButton button = null;
    JButton outButton = null;
    JButton generateExcelButton = null;
    JLabel inputLabel = null;
    JLabel outLabel = null;
    JLabel tipsLabel = null;
    JScrollPane scrollPane = null;
    public MyFrame(String title) {
        super(title);
        setSize(600, 360);
        Container container = getContentPane();
        inputLabel = new JLabel("输入路径:");
        inputLabel.setBounds(10,20,60,24);
        container.add(inputLabel);
        outLabel = new JLabel("输出路径:");
        outLabel.setBounds(10,54,60,24);
        container.add(outLabel);
        tipsLabel = new JLabel("等待合并！");
        tipsLabel.setBounds(10,178,280,50);
        tipsLabel.setFont( new Font("楷体",Font.BOLD,36));
        tipsLabel.setForeground(Color.red);
        container.add(tipsLabel);
        textField = new JTextField();
        textField.setBounds(80, 20, 150, 24);
        textField.setText("D:\\demo");
        
        container.add(textField);
        outTextField = new JTextField();
        outTextField.setBounds(80, 51, 150, 24);
        SimpleDateFormat sf3 = new SimpleDateFormat("MMdd");
        outTextField.setText("D:\\".concat(sf3.format(new Date())));
        container.add(outTextField);
        button = new JButton("选择合并路径");
        button.setToolTipText("选择有效的路径");
        button.setBounds(280, 20, 140, 24);
        container.add(button);
        outButton = new JButton("选择输出路径");
        outButton.setBounds(280, 51, 140, 24);
        container.add(outButton);
        generateExcelButton = new JButton("一键合并");
        generateExcelButton.setToolTipText("这是合并按钮,一键合并");
        generateExcelButton.setFont(new Font("宋体",Font.BOLD,24));
        generateExcelButton.setBounds(280, 100, 140, 78);
        container.add(generateExcelButton);
        
       
        
        setLayout(null);
        setVisible(true);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
       // setAlwaysOnTop(true);
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                onChoosePathClick();
            }
        });

        outButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                onOutButtonClick();
            }
            });
        generateExcelButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                combineButtonClick();
            }
        });
    }

    private  void combineButtonClick(){
            	try {
                    LuJunCombine.combine(textField.getText(),outTextField.getText());
                    tipsLabel.setText("合并完成!");
                    tipsLabel.setForeground(Color.blue);
                    Timer timer = new Timer();
                    TimerTask timerTask = new TimerTask(){

                      @Override
                      public void run() {
                        tipsLabel.setText("等待合并!");
                          tipsLabel.setForeground(Color.red);
                      }
                  };
                  timer.schedule(timerTask,9000);
            	} catch (Exception e) {
                    e.printStackTrace();
                }

            }

    private void onOutButtonClick() {
        String path1 =  outTextField.getText();
        if(path1.equals("")){
            Object[] options = { "OK", "CANCEL" };
            JOptionPane.showOptionDialog(null, "您还没有输入", "提示", JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE,
                    null, options, options[0]);
            return;
        }
        JFileChooser jfc = new JFileChooser();
        jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
        jfc.showDialog(new JLabel(), "选择");
        File file = jfc.getSelectedFile();
        if(file.isDirectory()) {
            System.out.println("文件夹:" + file.getAbsolutePath());
            outTextField.setText(file.getAbsolutePath());
        }else if (file.isFile()) {
            System.out.println("文件:"  + file.getAbsolutePath());
        }
        System.out.println(jfc.getSelectedFile().getName());

    }

    private void onChoosePathClick(){
     String path =   textField.getText();
     if(path.equals("")){
         Object[] options = { "OK", "CANCEL" };
         JOptionPane.showOptionDialog(null, "您还没有输入", "提示", JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE,
                 null, options, options[0]);
         return;
     }
        JFileChooser jfc = new JFileChooser();
        jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
        jfc.showDialog(new JLabel(), "选择");
        File file = jfc.getSelectedFile();
        if(file.isDirectory()) {
            System.out.println("文件夹:" + file.getAbsolutePath());
            textField.setText(file.getAbsolutePath());
        }else if (file.isFile()) {
            System.out.println("文件:"  + file.getAbsolutePath());
        }
        System.out.println(jfc.getSelectedFile().getName());
    }

}

